# Maintainer: TBK <alpine@jjtc.eu>
# Contributor: TBK <alpine@jjtc.eu>
# Contributor: Leonardo Arena <rnalrd@alpinelinux.org>
# Contributor: AdamRushad <2429990+adamrushad@users.noreply.github.com>
pkgname=unifi
_pkgname=UniFi
pkgver=5.13.32
pkgrel=0
pkgdesc="Ubiquiti UniFi Network Controller"
url="http://wiki.ubnt.com/UniFi_FAQ"
arch="noarch"
license="UniFi-EULA"
depends="openjdk8-jre-base java-snappy mongodb nss"
install="$pkgname.pre-install"
pkgusers="$pkgname"
pkggroups="$pkgname"
options="!check" # No test suite
subpackages="$pkgname-doc $pkgname-openrc"
source="$pkgname-$pkgver.zip::https://dl.ubnt.com/unifi/$pkgver/$_pkgname.unix.zip
	$pkgname.initd
	$pkgname.confd
	$pkgname.logrotated
	mongo_prune.js
	unifi_db_prune.cron
	"
builddir="$srcdir/$_pkgname"

prepare() {
	default_prepare
	# removing libubnt_webrtc_jni.so which disables rtc support
	# which disables cloud features (afaik)
	rm -rf "$builddir"/lib/native
}

package() {
	# unifi wants everything in a base directory but we dont
	local basedir="$pkgdir/usr/lib/$pkgname"
	local datadir="$pkgdir/var/lib/$pkgname"
	local webapps="$pkgdir/usr/share/webapps"

	mkdir -p $basedir/bin $datadir $webapps

	install -Dm644 "$srcdir"/$pkgname.logrotated \
		"$pkgdir"/etc/logrotate.d/$pkgname
	install -Dm755 "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd \
		"$pkgdir"/etc/conf.d/$pkgname
	install -Dm644 readme.txt \
		"$pkgdir"/usr/share/doc/$pkgname/readme.txt

	cp -r webapps $webapps/$pkgname

	local dir; for dir in dl lib conf; do
		cp -r $dir "$basedir"/
	done

	local dir; for dir in work data; do
		install -dm 755 -o $pkgname -g $pkgname "$datadir/$dir"
	done

	# link things back to basedir
	ln -sf /usr/bin/mongod $basedir/bin/mongod
	ln -sf /var/log/$pkgname $basedir/logs
	ln -sf /run/$pkgname $basedir/run
	ln -sf /usr/share/webapps/$pkgname $basedir/webapps
	ln -sf /var/lib/$pkgname/data $basedir/data
	ln -sf /var/lib/$pkgname/work $basedir/work

	# overwrite with local snappy but
	# first we got to figure out the version
	local file=$(ls lib/snappy-java*)
	local name=${file%.*}
	local version=${name##*-}
	# the java-snappy pkg has a symlink so we use that
	ln -sf /usr/share/java/snappy-java.jar \
		"$basedir"/lib/snappy-java-$version.jar

	# make sure datadir is writable by our user
	chown -R $pkgname:$pkgname $datadir

	# tell search engines to do not index
	echo -e "User-Agent:*\nDisallow:/" > "$webapps"/$pkgname/ROOT/robots.txt

	# install purging DB job (disabled by default)
	install -Dm644 "$srcdir"/mongo_prune.js "$pkgdir"/etc/unifi/mongo_prune.js
	install -Dm755 "$srcdir"/unifi_db_prune.cron "$pkgdir"/etc/periodic/weekly/unifi_db_prune
}

sha512sums="f1258d4f8fa73f43385260245ca31ee5d159c99db005609a22b09cbe551a886ebf685069bf95be5a0609335fe9d7290bc04158eb2430bdaa238aa7a13da681ab  unifi-5.13.32.zip
9763e2d7e843b9651d1712ec455cc5aa3efc16c71569cd3b2b470624ba2539efbc00294cd027cff15b21846c2a2f59417174bf59cfff76636fbb16483a691c1b  unifi.initd
d339555a91de7488badbedf8a4c85cff878e7d0720a8cf6a8340f51f3666dcf4878b47a1fff4c9c2846d7af140d11e48e898f8c4dba1f81c1004b76a81f0821e  unifi.confd
9e54d9e1c720b8e50c9af9363105f6ea9ff2cffff7dc67477a7701aacf21ba977424fe9fbaba6a00d5f64310a2e2517e9328a7acb91a4bc06ed237139a8e0d9b  unifi.logrotated
3eec528077af9b32aa065610b4ffc50537a1c851c84624eaa63287787d853c0f8c953a97bd58bc6c2cab7f9f26eaff8c8b2815a84454169f7e0d96b94dfc91ee  mongo_prune.js
25d66b144403676bcb6142f4942b62c6f969a6b359ccb4dd4fcb826b1e57f902179d01182369704970b2d6094b8b3789fd375c17cec908cac53f7abf01fb2e1b  unifi_db_prune.cron"
